package main

import (
	"fmt"
)

//func reverse(x int) int {
////	if x < -2147483648 || x > 2147483647 {
////		return 0
////	}
////	n, a := 0, 1
////	if x < 0 {
////		x = -x
////		a = -1
////	}
////
////	nStr := strconv.Itoa(x)
////	runes := []rune(nStr)
////	// 反转 []rune
////	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
////		runes[i], runes[j] = runes[j], runes[i]
////	}
////	// 将 []rune 转换回字符串
////	n, _ = strconv.Atoi(string(runes))
////	if n*a < -2147483648 || n*a > 2147483647 {
////		return 0
////	} else {
////		return n * a
////	}
////}

func reverse(x int) int {
	n := 0
	for x != 0 {
		n = n*10 + x%10
		x /= 10
	}
	if n < -2147483648 || n > 2147483647 {
		return 0
	}
	return n

}

func main() {
	fmt.Println("0:   ", reverse(0))
	fmt.Println("21:   ", reverse(120))
	fmt.Println("321:   ", reverse(123))
	fmt.Println("-321:   ", reverse(-123))

}
